package com.zhangyang.esper.one;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import com.espertech.esper.event.bean.BeanEventBean;
import com.espertech.esper.event.map.MapEventBean;

/**
 * 事件模型监听
 *
 * 实现UpdateListener接口，来定义事件的后置处理过程
 */
public class AppleListener implements UpdateListener {

    @Override
    public void update(EventBean[] newEvents, EventBean[] oldEvents) {
        if (newEvents != null) {
//            Double avg = (Double) newEvents[0].get("avg(price)");
//            System.out.println("Apple's average price is " + avg);
            for (EventBean eventBean : newEvents) {
                if (eventBean instanceof MapEventBean) {
                    MapEventBean map = (MapEventBean)eventBean;
                    System.out.println(map.get("name") + " : " + map.get("sum(price)") + " id:" + map.get("id"));
                } else if (eventBean instanceof BeanEventBean) {
                    BeanEventBean beanEventBean = (BeanEventBean)eventBean;
                    System.out.println("id:" + beanEventBean.get("id") + " name:" + beanEventBean.get("name") + " price:" + beanEventBean.get("price"));
                } else {
                    String name = (String) eventBean.get("name");
                    Double sumPrice = (Double) eventBean.get("sum(price)");
                    System.out.println(name + " : " + sumPrice);
                }
            }


        }
    }
}